package org.smart4j.chapter1;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/hello") //使用注解并配置请求路径，对外发布Servlet服务
public class HelloServlet extends HttpServlet{
    //继承HttpServlet类，让它成为一个Servlet类
    @Override
    //覆盖父类的doget方法，用于接收get请求
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        //在doget方法中获取系统当前时间，并将其放入HttpServletRequest对象中
        //最后转发到WEB-INF/jsp/hello.jsp
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentTime = dateFormat.format(new Date());
        req.setAttribute("currentTime", currentTime);
        req.getRequestDispatcher("/WEB-INF/jsp/hello.jsp").forward(req,
                resp);

    }
}
